<?php

namespace App\Admin\Controllers;

use App\Models\Area;
use App\Models\Scene;
use App\Models\Type;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class SceneController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('景区基础信息');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Scene::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->disableExport();
            $grid->id('ID')->sortable();
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                // 在这里添加字段过滤器
                $filter->like('title', '名称');
                $filter->equal('area_id', '区县')->select(Area::pluck('title','id'));
                $filter->equal('is_docking', '是否对接')->select([1=>'是',2=>'否']);

            });
            $grid->title('名称')->editable();
            $grid->level('等级')->editable('select', config('options.scene_level'));
            $grid->business_license('营业执照');
            $grid->latlng('坐标');
            $grid->area_id('区县')->display(function ($val){
                return Area::all()->pluck('title','id')[$val];
            });
            $grid->type_id('景点类型')->display(function ($val){
                return Type::all()->pluck('title','id')[$val];
            });
            $grid->founded_on('入驻时间');
            $grid->current_flow('当前人流上报')->editable();
            $grid->status('运营状态')->display(function ($val){
                return $val?config('options.status')[$val]:'运营中';
            });
            $grid->is_docking('是否对接')->switch([
                'on'  => ['value' => 1, 'text' => '是'],
                'off' => ['value' => 0, 'text' => '否'],
            ]);
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Scene::class, function (Form $form) {
            $form->select('area_id','区县选择')->options(Area::all()->pluck('title','id'));
            $form->select('type_id','景点类型')->options(Type::all()->pluck('title','id'));

            $form->text('title','名称')->rules(function ($form) {
                // 如果不是编辑状态，则添加字段唯一验证
                if (!$title = $form->model()->title) {
                    return 'unique:scenes';
                }
            },['unique'=>'该景区已添加']);
            $form->image('cover','图片')->uniqueName()->removable();
            $form->text('scene_area','面积');
            $form->text('business_license','营业执照');
            $form->tags('latlng','经纬度');
            $form->select('level','等级')->options(config('options.scene_level'));
            $form->switch('is_docking', '是否对接')->states([
                'on'  => ['value' => 1, 'text' => '是'],
                'off' => ['value' => 0, 'text' => '否'],
            ]);
            $form->hidden('current_flow');
            $form->datetime('founded_on','入驻时间');
        });
    }
}
